package com.filldream.fastboot.common.okutil.stock;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.filldream.fastboot.common.util.JsonUtil;
import com.filldream.fastboot.common.util.RandomUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.ObjectUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * 股票基本信息
 * 参考博客：
 * https://blog.csdn.net/zchill/article/details/121303871
 * 参考API：
 * https://tpdog.com/api/stock_his/daily?code=sz.002456&start=2024-04-08&end=2024-04-09
 * https://tpdog.com/api/board/bill?date=2024-01-17
 * http://tpdog.com/api/pool/limitup/list?date=2024-01-17
 * http://tpdog.com/api/indicator/macd?code=sz.000001&period=10&macd_long=26&macd_short=12&macd_mid=9&date=2024-01-17
 * https://qt.gtimg.cn/q=sz002456
 * https://qt.gtimg.cn/q=s_sz002456
 * https://qt.gtimg.cn/q=ff_sz002456
 */
@Data
@Accessors(chain = true)
public class StockInfoDay {

    /** 日期0 **/
    private String data;

    /** 开盘价1 **/
    private String openPrice;

    /** 收盘价2 **/
    private String closePrice;

    /** 最高价3 **/
    private String highPrice;

    /** 最低价4 **/
    private String lowPrice;

    /** 总手数5 **/
    private String turnover;

    /** 成交额6 **/
    private String amount;

    /** 振幅7 **/
    private String amplitude;

    /** 涨幅8 **/
    private String change;

    /** 换手率9 **/
    private String turnoverRate;

    /** url返回结果 **/
    private String urlResult;

    public static List<StockInfoDay> getDayList(String code,String endDate){
        code = code.replace("sz","0.");
        String url = "https://push2his.eastmoney.com/api/qt/stock/kline/get?"
                +"secid="+code
                + "&end=" + endDate
                +"&ut=fa5fd1943c7b386f172d6893dbfba10b"
                +"&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&klt=101&fqt=1&lmt=210&cb=quote_jp6";
        String urlResult = HttpUtil.get(url);
        urlResult = urlResult.substring( urlResult.indexOf("(") + 1, urlResult.length()-2);

        JSONObject jsonObject = JSONObject.parseObject(urlResult);
        String listJson = jsonObject.getJSONObject("data").getJSONArray("klines").toJSONString();
        return initList(listJson);
    }

    public static List<StockInfoDay> getDayList(String code){
        return getDayList(code,DateUtil.format(new Date(),"yyyyMMdd"));
    }

    //写一个方法，传入String,根据String逗号分隔符获取List<String>，然后一一对应赋值给StockInfoDay对象
    public static StockInfoDay init(String itemStr){
        StockInfoDay stockInfoDay = new StockInfoDay();
        String[] split = itemStr.split(",");
        stockInfoDay.setData(split[0]);
        stockInfoDay.setOpenPrice(split[1]);
        stockInfoDay.setClosePrice(split[2]);
        stockInfoDay.setHighPrice(split[3]);
        stockInfoDay.setLowPrice(split[4]);
        stockInfoDay.setTurnover(split[5]);
        stockInfoDay.setAmount(split[6]);
        stockInfoDay.setAmplitude(split[7]);
        stockInfoDay.setChange(split[8]);
        stockInfoDay.setTurnoverRate(split[9]);
        return stockInfoDay;
    }

    public static List<StockInfoDay> initList(String str){
        List<String> strList = JsonUtil.fromJosnArray(str, String.class);
        List<StockInfoDay> stockInfoDayList = new ArrayList<>();
        for (String itemStr : strList) {
            stockInfoDayList.add(StockInfoDay.init(itemStr));
        }
        return stockInfoDayList;
    }

    public static BigDecimal getPrice(List<StockInfoDay> list){
        //创建一个变量a，以list第一个amount为a,然后从第二个开始遍历，a=amount-a
        BigDecimal a = new BigDecimal(list.get(0).getAmount()).subtract(new BigDecimal(list.get(1).getAmount()));
        for (int i = 2; i < list.size(); i++) {
            a = a.add( new BigDecimal(list.get(i).getAmount()).subtract(new BigDecimal(list.get(i-1).getAmount())));
        }
        return a;
    }


    public static void main(String[] args) {
        StockOrder stockOrder = new StockOrder().setPrice("9.795").setCount("100");
        StockOrder stockOrder2 = new StockOrder().setPrice("9.1").setCount("100");
        System.out.println(stockOrder.getNeedPrice("0"));
        System.out.println(StockOrder.doT( Arrays.asList(stockOrder, stockOrder2),"9.31"));
    }




}
